/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Manish Mehta 
 												
Class							:				PeasantIdleFix
 
Function						: 				A class for holding a 
												fix for situations when 
												a peasant is idle
****************************************************************************/
package behavioradaptation.fixes;


import java.util.List;
import java.util.Vector;

import planninglanguage.plans.BasicOperatorPlan;
import planninglanguage.plans.Plan;
import planninglanguage.plans.ResourceLocationBOPlan;
import trace.Entry;
import trace.Trace;
import base.WargusGameState;
import base.WargusStateImporter;
import behavioradaptation.analysisdata.AbstractedTrace;
import behavioradaptation.analysisdata.FixData;
import behavioradaptation.analysisdata.IdleData;
import behavioradaptation.analysisdata.PlanFailuresData;
import behavioradaptation.reasoner.GameStateHelpers;
import behavioradaptation.reasoner.TraceHelpers;
import cbr.Coordinate;
import cbr.episodicCBR.EpisodicCBR;
public class PeasantIdleFix {
	public static void fix(List<PlanFailuresData> a_planFailuresList, AbstractedTrace a_abstractedtrace, FixData a_fxdata, EpisodicCBR a_cbr) throws Exception
	{
		boolean first_time = true;
		Vector <IdleData> a_idleDataList = a_abstractedtrace.getIdleData();		
		Trace a_trace = a_abstractedtrace.getTrace();	
		int x, y;
		x = 13; y = 16;
		Entry ent = null;
		for(int i = 0; i < a_idleDataList.size(); i++)
		{
			ResourceLocationBOPlan rbplan = null;
			IdleData id = (IdleData)a_idleDataList.get(i);
			int index = id.getStartIndex();
			if((index + 3) < a_trace.size())
				ent = (Entry)a_trace.get(index+3);			
			else	
				ent = (Entry)a_trace.get(index);	
			Plan plan = ent.getPlan();
			WargusGameState game_state = ent.getGameState();
			/*
			 * If the peasant unit is idle then currently the fix 
			 * is to add a resource gathering behavior 
			 */
			if(id.getUnit().getType()==WargusStateImporter.unitTypeToInteger("peasant") && 
			   (ent.getPlan() instanceof BasicOperatorPlan))
			{
				if(!first_time)
				{	
					// Currently all the units are sent to mine gold
					// Probably should carry out harvesting wood and gold mine 
					// with some probability 
					List <Coordinate> pairlist = GameStateHelpers.GoldMinesXY(game_state);
					if(pairlist.size() > 0)
					{
						x = ((Coordinate)pairlist.get(0)).x;
						y = ((Coordinate)pairlist.get(0)).y;
						
					}	
					rbplan = new ResourceLocationBOPlan(id.getUnit().getUnitID(), x, y , null, null, null);
					// Modify the case base with the modified plan 
					TraceHelpers.modifyCaseBase(plan, a_trace, rbplan,ent, false, a_cbr);
				}	
				else
					first_time = false;
			}		
		}	
	}
}
